/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.outpatient.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.his.domain.Doctor;
import me.zhengjie.modules.his.domain.Report;
import me.zhengjie.modules.his.outpatient.service.ReportService;
import me.zhengjie.modules.his.outpatient.service.dto.ReportQueryCriteria;
import me.zhengjie.modules.his.recordsCenter.repository.DoctorRepository;
import me.zhengjie.modules.his.recordsCenter.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Liu
* @date 2022-11-05
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "his:挂号表管理")
@RequestMapping("/api/report")
public class ReportController {

    private final ReportService reportService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('report:list')")
    public void exportReport(HttpServletResponse response, ReportQueryCriteria criteria) throws IOException {
        reportService.download(reportService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询his:挂号表")
    @ApiOperation("查询his:挂号表")
    @PreAuthorize("@el.check('report:list')")
    public ResponseEntity<Object> queryReport(ReportQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(reportService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增his:挂号表")
    @ApiOperation("新增his:挂号表")
    @PreAuthorize("@el.check('report:add')")
    public ResponseEntity<Object> createReport(@Validated @RequestBody Report resources){
        return new ResponseEntity<>(reportService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改his:挂号表")
    @ApiOperation("修改his:挂号表")
    @PreAuthorize("@el.check('report:edit')")
    public ResponseEntity<Object> updateReport(@Validated @RequestBody Report resources){
        reportService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除his:挂号表")
    @ApiOperation("删除his:挂号表")
    @PreAuthorize("@el.check('report:del')")
    public ResponseEntity<Object> deleteReport(@RequestBody Integer[] ids) {
        reportService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Autowired
    DoctorRepository doctorRepository;

    /**
     * 根据科室表查询所有的医生
     * 1.这个接口应该放在Doctor表的CRUD
     * 2.先选择科室，在选择医生。医生确定后闹到挂号费用。
     * @param departmentId
     * @return
     */
    @GetMapping("/{departmentId}")
    @Log("测试his:")
    @ApiOperation("测试his:挂号表")
    public List<Doctor> queryDoctors(@PathVariable Integer departmentId) {
        List<Doctor> byDepartmentId = doctorRepository.findByDepartmentId(departmentId);
        return byDepartmentId;
    }


}
